package com.andyliu.configer;

import com.andyliu.interceptor.ControllerInterceptor;
import com.andyliu.interceptor.LoginInterceptor;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * ClassName: MvcConfiger
 * Description: TODO
 * date: 2021/1/18 5:30 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Configuration
//@EnableWebMvc
@ComponentScan("com.andyliu")
@EnableAspectJAutoProxy
public class MvcConfiger  implements WebMvcConfigurer {
    @Autowired
    private ControllerInterceptor controllerInterceptor;
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("/druid/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

    }
    @Bean
    public DefaultKaptcha getKaptcha(){
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties pro = new Properties();
        pro.put("kaptcha.border","yes");
        pro.put("kaptcha.border.color","105,179,90");
        pro.put("kaptcha.textproducer.font.color","blue");
        pro.put("kaptcha.image.width","125");
        pro.put("kaptcha.image.height","40");
        pro.put("kaptcha.textproducer.font.size","30");
        pro.put("kaptcha.session.key","code");
        pro.put("kaptcha.textproducer.char.length","4");
        pro.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
        Config config = new Config(pro);
        kaptcha.setConfig(config);
        return kaptcha;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(controllerInterceptor).addPathPatterns("/**");
        List<String> excludePath = new ArrayList<>();
        excludePath.add("/login");
        excludePath.add("/*.html");
        excludePath.add("/verification");
        excludePath.add("/swagger-resources/**");
        excludePath.add("/webjars/**");
        excludePath.add("/v2/**");
        excludePath.add("/log/**");
        excludePath.add("/druid/**");
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns(excludePath);
    }
}
